import { defineStore } from 'pinia'
import { ref } from 'vue'
import { parse, stringify } from 'zipson'
import { getStuInfoAPI } from '../../api/user'
import { getStuMesListAPI } from '../../api/message'

export const useStuStore = defineStore(
  'student',
  () => {
    const stuInfo = ref({})
    const mesList = ref([])

    const getStudent = async () => {
      const uid = localStorage.getItem('uid')
      const { data: res } = await getStuInfoAPI(uid)
      if (res.status === 200) {
        stuInfo.value = res.data
      }
    }

    const getMesList = async () => {
      const uid = localStorage.getItem('uid')
      const { data: res } = await getStuMesListAPI(uid)
      if (res.status === 200) {
        mesList.value = res.data
      }
    }

    const setMesList = (val) => {
      mesList.value = val
    }

    const setStuInfo = (val) => {
      stuInfo.value = val
    }

    return {
      stuInfo,
      setStuInfo,
      getStudent,
      mesList,
      setMesList,
      getMesList
    }
  },
  {
    persist: {
      key: 'u',
      paths: ['stuInfo'],
      serializer: {
        deserialize: parse,
        serialize: stringify
      }
    }
  }
)
